<{capture name="header"}>
<style>
.order-box{ height:auto; padding:10px; margin:5px 20px; border:1px solid #ddd}
.order-box li{ padding:3px 0}
.order-tr{  font-size:14px; font-weight:bold; border-bottom:1px solid #ddd}
.table-border{ margin:10px 0; border-top:2px solid #333;border-bottom:2px solid #333}
.order-box td{ padding:3px 5px}
.order-font{ font-size:14px; font-weight:bold; padding:0 5px; clear:both}
</style>
<{/capture}>

<script type="text/javascript">
var err = <{$err}>;
if (err==true){
	new Dialog(new Element("div.tableform",'<div class="division">部分发货单已被合并或者拆分</div><div class="table-action"><{button label="关闭" onclick="re_finder();"}></div>'),{
        title:'提示',
        width:230,
        height:130,
        modal:true,
        resizeable:false});
}

function re_finder(){
    opener.finderGroup['<{$env.get.finder_id}>'].unselectAll();
    opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(400,opener.finderGroup['<{$env.get.finder_id}>']);
    window.close();
}
var count = <{$count}>;

function enterEMSNo(btn){
	//向下
	var value = btn;
	while (true){
        var aa = value+1;
        if ($("txt"+aa)){
            $("txt"+aa).value = getEMSNext($('txt'+value).value);
            value++;
        }else {
            break;
        }
    }
    //向上
    value = btn;
	while (true){
        var aa = value-1;
        if ($("txt"+aa)){
            $("txt"+aa).value = getEMSPrev($('txt'+value).value);
            value++;
        }else {
            break;
        }
    }
    return ;
}

function editBtn(btn){
    var value = btn;
    if ($("txt"+value).value == ''){
        alert("请扫描快递单号");
        return;
    }
    var str = $("txt"+value).value;
    end = str.substr(-1);
    if(isNaN(end)){
        if (isEMSNo(str)){
            enterEMSNo(btn);
            return ;
        }
        //alert("快递单号最后一位必须为数字才可执行");
        return;
    }
    num = str.match(/\d+\b/g);
    len = str.lastIndexOf(num);
    start = str.substring(0,len);
    n = new Number(num)+1;
    while (true){
        var aa = value+1;
        if ($("txt"+aa)){
            $("txt"+aa).value = start+n;
            n++;
            value++;
        }else {
            break;
        }
    }
    editBtn2(btn);
    return;
}
function editBtn2(value){
    if ($("txt"+value).value == ''){
        alert("请扫描快递单号");
        return;
    }
    var str = $("txt"+value).value;
    end = str.substr(-1);
    if(isNaN(end)){
        alert("快递单号最后一位必须为数字才可执行");
        return;
    }
    num = str.match(/\d+\b/g);
    len = str.lastIndexOf(num);
    start = str.substring(0,len);
    n = new Number(num)-1;
    while (true){
        var bb = value-1;
        if ($("txt"+bb)){
            $("txt"+bb).value = start+n;
            n--;
            value--;
        }else {
            break;
        }
    }
    return;
}
function check(){
    var value = 1;
    for (i=1;i<=count;i++){
        if ($("txt"+i).value == ''){
            alert("请填写完所有发货单对应的快递单号");
            return false;
        }
    }
    $('form1').submit();
}
$("cc").addEvent('change',function(e){
    if (this.checked){
        $ES("input[id^=bbaa]").set('styles',{'display':''});
        $('sp_1').set('text','开启上下关联');
    }else {
        $ES("input[id^=bbaa]").set('styles',{'display':'none'});
        $('sp_1').set('text','关闭上下关联');
    }
});
$ES("input[id^=txt]").each(function(el,i){
    el.addEvent('keydown',function(e){
        if(e.code==13){
                var a = i+1;
                if ($ES("input[id^=txt]").length != a){
                        $ES("input[id^=txt]")[a].focus();
                }else if($ES("input[id^=txt]").length == a){
					check();
				}
            return false;
        }
    });
});

$('form1').store('target',{
    onRequest:function(){
       $('b2').set('disabled', 'true');
    },
    onComplete:function(jsontext){
       var json = Json.evaluate(jsontext);
       if (typeof(json.error) != 'undefined'){
           $('b2').set('disabled', '');
       }else{
           $('b2').set('disabled', 'true');
           opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(400,opener.finderGroup['<{$env.get.finder_id}>']);
           window.close();
       }
    }
});

function getEMSLastNum(no){
    var v = new Number(no.substr(2,1)) * 8;
    v += new Number(no.substr(3,1)) * 6;
    v += new Number(no.substr(4,1)) * 4;
    v += new Number(no.substr(5,1)) * 2;
    v += new Number(no.substr(6,1)) * 3;
    v += new Number(no.substr(7,1)) * 5;
    v += new Number(no.substr(8,1)) * 9;
    v += new Number(no.substr(9,1)) * 7;
    v = 11 - v % 11;
    if (v == 10)
        v = 0;
    else if (v == 11)
        v = 5;
    return v.toString();
}

function getEMSPrev(no){
    var serialNo = new Number(no.substr(2,8));
    if (serialNo > 0)
        serialNo--;
    strSerialNo = pad(serialNo, 8);
	temp = no.substr(0,2) + strSerialNo + no.substr(10);
	temp = no.substr(0,2) + strSerialNo + getEMSLastNum(temp) + no.substr(11);
	return temp;
}

function getEMSNext(no){
    var serialNo = parseInt(no.substr(2,8));
    if (serialNo < 99999999)
        serialNo++;
    strSerialNo = pad(serialNo, 8);
    temp = no.substr(0,2) + strSerialNo + no.substr(10);
    temp = no.substr(0,2) + strSerialNo + getEMSLastNum(temp) + no.substr(11);
    return temp;
}

function isEMSNo(no){
    if (no.length != 13){
        return false;
    }
    if (getEMSLastNum(no) == no.substr(10,1))
        return true;
    else
        return false;
}

function pad(num, n) {
    return Array(Math.abs((''+num).length-(n+1))).join(0)+num;
}

</script>
<{if $err=='false'}>
<div class="">
<div class="order-box">
<form name="form1" id="form1" method="POST" action="index.php?app=ome&ctl=admin_receipts_print&act=insertExpress&finder_id=<{$env.get.finder_id}>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-bottom:1px solid #ddd">
<tr class="order-tr"><td><b>打印快递单</b></td></tr>
<tr> <td class="d_th">请扫描快递单号：(注意：快递单号必须为数字)&nbsp;</td><td><input type="checkbox" name="c1" id="cc" /><span id="sp_1">关闭上下关联</span></td><td width="60%"></td></tr>
<{foreach from=$idd key=key item=item}>
<tr><td class="d_td" ><{$item.delivery_bn}>:</td><td>
    <input type="text" vtype="number&&required" name="id[<{$item.delivery_id}>]" id="txt<{++$key}>" value="<{$logid[$item.delivery_id]}>" />&nbsp;&nbsp;</td>
    <td><{if count($idd) != 1}><input type="button" style="display:none" id="bbaa<{$key}>" class="btn" value="上下关联" onClick="editBtn(<{$key}>)"/><{/if}></td>
    <td></td>
  </tr>
<{/foreach}>
<tr> <td class="d_th"><{button label="确认" class="btn-primary" type="button" onClick="check()" id="b2" }>&nbsp;</td>
<td></td><td></td></tr>

</table>
</form>


<input type="hidden" name='printname' id='printname' value="<{$vid}>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
 <div class="note">
 <h5>快递单打印预览：</h5>
    上偏移:<{$prt_tmpl.prt_tmpl_offsety|default:0}>mm,
    左偏移:<{$prt_tmpl.prt_tmpl_offsetx|default:0}>mm,
    纸张宽:<{$prt_tmpl.prt_tmpl_width}>mm,
    纸张高:<{$prt_tmpl.prt_tmpl_height}>mm,
 </div>
 <div id="dly_printer" style="height:<{$prt_tmpl.prt_tmpl_height*$dpi/25.4*$order_number}>px;width:<{$prt_tmpl.prt_tmpl_width*$dpi/25.4}>px;">
</div>
</td>
  </tr>
</table>
</div>
</div>
<{/if}>

<textarea id="print_data" style="display:none">
<{$data}>
</textarea>

<script>

function changePrint()
{
    new Dialog(new Element("div.tableform",{html:'<div id="pause" class="division">正在提交...<{img app="desktop" src="loading.gif"}></div><div class="table-action"><{button label="关闭" onclick="re_finder();"}></div>'}),{
        title:'提示',
        width:230,
        height:130,
        modal:true,
        resizeable:false}
    );
    var printname= $('printname').value;
    new Request({url:'index.php?app=ome&ctl=admin_receipts_print&act=setPrintStatus',method:'post',data:'type=express&str='+printname,
        onSuccess:function(json){
          if (json == 'true'){
              $('pause').getParent('.dialog').retrieve('instance').close();
              $('dly_printer_flash').doPrint();
          }else {
              $('pause').set('text',json);
          }
        }
    }).send();
}

var swf = new Swiff('<{$base_dir}>/app/ome/upload/images/printermode.swf?<{$smarty.now}>', {
    width:  '100%',
    height: '100%',
    params:{wMode:false},
    id:'dly_printer_flash',
    container: $('dly_printer'),
    vars:{
    xml:'<{$prt_tmpl.prt_tmpl_data}>',
    data:$("print_data").value <{if $tmpl_bg}>,
    bg:'<{$tmpl_bg}>'<{/if}>,
    order_number:'<{$order_number}>',
    offset_y:'<{$prt_tmpl.prt_tmpl_offsety|default:0}>',
    offset_x:'<{$prt_tmpl.prt_tmpl_offsetx|default:0}>',
    page_w:'<{$prt_tmpl.prt_tmpl_width}>',
    page_h:'<{$prt_tmpl.prt_tmpl_height}>',
    copyright:'shopex'}
});


/*flash call*/
var printermode_complete_callback = function(){
        
        $('dly_printer').empty().set('html',"<div class='success'>任务已成功发送至打印机!</div>");
        $('doprint_btn').dispose();

}

var printermode_ready_callback = function(){


}
</script>
<{capture name="footbar"}>
<div class="table-action">
    <{button label="打印快递单" onclick="changePrint()" id="doprint_btn"}>
</div>
<{/capture}>